package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.*;
import com.zhentao.pojo.*;
import com.zhentao.service.ScenesService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author q1812
* @description 针对表【scenes】的数据库操作Service实现
* @createDate 2025-03-21 16:55:52
*/
@Service
public class ScenesServiceImpl extends ServiceImpl<ScenesMapper, Scenes>
    implements ScenesService{

    @Resource
    private ScenesMapper scenesMapper;
    @Resource
    private ScenesDevicesMapper scenesDevicesMapper;
    @Resource
    private DevicesMapper devicesMapper;
    @Resource
    private ChufatiaojianMapper chufatiaojianMapper;
    @Resource
    private ChangjingshebeichufaMapper changjingshebeichufaMapper;

    //    查询场景
    @Override
    public List<Scenes> findlist(Integer userid,String name) {
        Scenes scenes1 = scenesMapper.selectById(1);

        QueryWrapper<Scenes> queryWrapper=new QueryWrapper<>();

        queryWrapper.eq("user_id",userid);
        queryWrapper.like("scenes_name",name);


        List<Scenes> scenes = scenesMapper.selectList(queryWrapper);

        if (userid!=1){
            scenes.add(scenes1);
        }


        for (Scenes s:scenes){
            QueryWrapper<Chufatiaojian> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("scenes_id", s.getScenesId());
            List<Chufatiaojian> chufatiaojians = chufatiaojianMapper.selectList(queryWrapper1);
            s.setChufatioajians(chufatiaojians);

            QueryWrapper<Devices> queryWrapper2=new QueryWrapper<>();
            queryWrapper.eq("scenes_id",s.getScenesId());
            List<Devices> devices = devicesMapper.selectList(queryWrapper2);

            s.setDevices(devices);
        }


        return scenes;
    }


    //   详情里的设备
    @Override
    public Scenes xiangqing(Integer scenesid) {
        Scenes scenes = scenesMapper.selectById(scenesid);
        QueryWrapper<ScenesDevices> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("scenes_id",scenesid);
        List<ScenesDevices> scenesDevices1 = scenesDevicesMapper.selectList(queryWrapper);


        List<Integer> did=new ArrayList<>();
        for (ScenesDevices scenesDevices:scenesDevices1){
            did.add(scenesDevices.getDevicesId());
        }
        //        场景触发条件
        QueryWrapper<Chufatiaojian> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("scenes_id",scenesid);
        List<Chufatiaojian> chufatiaojians = chufatiaojianMapper.selectList(queryWrapper1);
        scenes.setChufatioajians(chufatiaojians);

        List<Devices> devices = devicesMapper.selectBatchIds(did);



///        场景设备触发条件
        for (Devices devices1:devices){
            QueryWrapper<Changjingshebeichufa> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("device_id",devices1.getId());
            queryWrapper2.eq("scenes_id",scenes.getScenesId());
            List<Changjingshebeichufa> changjingshebeichufas = changjingshebeichufaMapper.selectList(queryWrapper2);
            devices1.setChangjingshebeichufas(changjingshebeichufas);
        }


        scenes.setDevices(devices);

        System.err.println(scenes);
        return scenes;
    }



    //    添加
    @Override
    public String add(Scenes scenes) {
        scenes.setState(0);
        scenesMapper.insert(scenes);
        System.err.println(scenes);
        Chufatiaojian chufatiaojian=new Chufatiaojian();
        chufatiaojian.setName(scenes.getName());
        chufatiaojian.setScenesId(scenes.getScenesId());
        chufatiaojianMapper.insert(chufatiaojian);
        ScenesDevices scenesDevices=new ScenesDevices();
        scenesDevices.setScenesId(scenes.getScenesId());
        for (Integer a:scenes.getId()){
            scenesDevices.setDevicesId(a);
        }
        scenesDevicesMapper.insert(scenesDevices);
        return "添加成功";
    }

//    删除
    @Override
    public Integer del(Integer id) {

        Scenes scenes = scenesMapper.selectById(id);

        QueryWrapper<ScenesDevices> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("scenes_id",id);

        scenesDevicesMapper.delete(queryWrapper);

        QueryWrapper<Chufatiaojian> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("scenes_id",id);

        chufatiaojianMapper.delete(queryWrapper1);

        scenesMapper.deleteById(id);

        return 1;
    }

    @Override
    public void qidong(Integer sceneid, Integer id) {

        Scenes scenes = scenesMapper.selectById(sceneid);
        scenes.setState(id);
        scenesMapper.updateById(scenes);

        QueryWrapper<ScenesDevices>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("scenes_id",scenes.getScenesId());
        List<ScenesDevices> scenesDevices = scenesDevicesMapper.selectList(queryWrapper);

        List<Integer> did=new ArrayList<>();
        for (ScenesDevices scenesDevices1:scenesDevices){
            did.add(scenesDevices1.getDevicesId());
        }

        UpdateWrapper<Devices> updateWrapper=new UpdateWrapper<>();
        System.err.println(did);

        updateWrapper.set("status",id);
        updateWrapper.eq("id",did);
        devicesMapper.update(updateWrapper);
    }


}




